
package com.workcell.workdata.xthings.service.impl;


import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.workcell.workdata.xthings.constant.HardwareStateEnum;
import com.workcell.workdata.xthings.dto.BaseVo;
import com.workcell.workdata.xthings.entity.BaseUpperComputer;
import com.workcell.workdata.xthings.mapper.BaseUpperComputerMapper;
import com.workcell.workdata.xthings.service.BaseUpperComputerService;
import org.springframework.stereotype.Service;

/**
 * 阀门表
 *
 * @author pigx code generator
 * @date 2023-05-16 10:44:41
 */
@Service
public class BaseUpperComputerServiceImpl extends ServiceImpl<BaseUpperComputerMapper, BaseUpperComputer> implements BaseUpperComputerService {

    @Override
    public Page<BaseUpperComputer> vaguePage(BaseVo baseVo) {
        LambdaQueryWrapper<BaseUpperComputer> baseFreezerCondition = new QueryWrapper<BaseUpperComputer>().lambda()
                .and(ObjectUtil.isNotEmpty(baseVo.getKeyWord()), i -> i
                        .like(BaseUpperComputer::getName,baseVo.getKeyWord())
                        .or()
                        .like(BaseUpperComputer::getManufacturer,baseVo.getKeyWord()))
                .eq(ObjectUtil.isNotEmpty(baseVo.getState()),BaseUpperComputer::getState,baseVo.getState())
                .and(ObjectUtil.isNotEmpty(baseVo.getStartTime()) || ObjectUtil.isNotEmpty(baseVo.getEndTime())
                        , i -> i.between(BaseUpperComputer::getCreateTime,baseVo.getStartTime(),baseVo.getEndTime()));
        Page<BaseUpperComputer> page = new Page<>(baseVo.getPageNum(), baseVo.getPageSize());
        page.addOrder(OrderItem.desc("create_time"));
        return this.page(page,baseFreezerCondition);
    }

    @Override
    public Boolean updateStateById(BaseUpperComputer baseUpperComputer) {
        Assert.isTrue(ObjectUtil.isNotEmpty(baseUpperComputer.getId()),"ID不能为空");
        Assert.isTrue(ObjectUtil.isNotEmpty(baseUpperComputer.getState()),"状态不能为空");
        // 查询该阀门的状态
        BaseUpperComputer baseOld = this.getById(baseUpperComputer.getId());
        Assert.isTrue(ObjectUtil.isNotEmpty(baseOld),"无该条记录，请核实您的操作");
        if (HardwareStateEnum.INUSE.getCode().equals(baseOld.getState())
                || HardwareStateEnum.SUSPEND.getCode().equals(baseOld.getState())){
            if (HardwareStateEnum.DRAF.getCode().equals(baseOld.getState())){
                Assert.isTrue(false,"当状态为使用中或暂停中时，不能修改为草稿状态");
            }
        }
        return this.updateById(baseUpperComputer);
    }
}
